package com.lizhi.yun.sync.rsz.handler;/**
 * @program: hbsf_yun
 * @name RszPatientHandler
 * @description: 患者信息
 * @author: lizhi
 * @create: 2021-11-09 08:41
 */

import com.lizhi.common.yun.entity.RpcServicePOJO;
import com.lizhi.common.yun.enums.HeartBeartEnum;
import com.lizhi.utils.StringUtils;
import com.lizhi.yun.db.rsz.dao.RszPatientDao;
import com.lizhi.utils.json.SamJsonUtil;
import com.lizhi.yun.db.rsz.entity.RszPatientEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *@program: hbsf_yun
 *@name RszPatientHandler
 *@description: 患者信息
 *@author: lizhi
 *@create: 2021-11-09 08:41
 */
@Slf4j
@Service
public class RszPatientSyncHandler {

    @Resource
    private RszPatientDao rszPatientDao;

    public String recv(String json){
        RpcServicePOJO rpcServicePOJO = new RpcServicePOJO();
        try {
            RszPatientEntity rszPatientEntity = SamJsonUtil.toObject(json, RszPatientEntity.class);
            log.info("康复磨砂卓患者信息上传:{}", rszPatientEntity);
            Map<String, Object> map = new HashMap<>();
            map.put("ptId", rszPatientEntity.ptId);
            map.put("hospitalId", rszPatientEntity.hospitalId);
            map.put("hospitalSystemId", rszPatientEntity.hospitalSystemId);
            List<RszPatientEntity> rszPatientEntities = rszPatientDao.selectByMap(map, null);
            if (rszPatientEntities.size() > 0){
                rszPatientDao.updateByIdSelective(rszPatientEntity);
            } else {
                rszPatientEntity.lid = StringUtils.newId();
                rszPatientDao.insertRecord(rszPatientEntity);
            }
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000000.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000000.getEnName());
        } catch (Exception e){
            e.printStackTrace();
        }
        return SamJsonUtil.toJson(rpcServicePOJO);
    }
}
